package com.bw.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.bw.service.TmsService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class TmsController {
    @Autowired
    TmsService tmsService;
    @RequestMapping("/sumTotal")
    public Object sumTotal(@RequestParam("nowDate") String nowDate ){
        List<Map> list = tmsService.sumTotal(nowDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONArray dataArray = new JSONArray();

        JSONObject dataObject = new JSONObject();
        dataObject.put("dimValue","");
        JSONArray indiArray = new JSONArray();
        JSONObject indiObject = new JSONObject();
        indiObject.put("value",list.get(0).get("total"));
        indiObject.put("rate_level","red");
        JSONObject nameObject = new JSONObject();
        nameObject.put("name","运输次数");
        indiArray.add(indiObject);
        indiArray.add(nameObject);
        dataObject.put("indicators",indiArray);
        dataArray.add(dataObject);


        JSONObject dataObject1 = new JSONObject();
        dataObject1.put("dimValue","");
        JSONArray indiArray1 = new JSONArray();
        JSONObject indiObject1 = new JSONObject();
        indiObject1.put("value",list.get(0).get("distance"));
        indiObject1.put("rate_level","green");
        JSONObject nameObject1 = new JSONObject();
        nameObject1.put("name","运输里程");
        indiArray1.add(indiObject1);
        indiArray1.add(nameObject1);
        dataObject1.put("indicators",indiArray1);
        dataArray.add(dataObject1);


        JSONObject dataObject2 = new JSONObject();
        dataObject2.put("dimValue","");
        JSONArray indiArray2 = new JSONArray();
        JSONObject indiObject2 = new JSONObject();
        indiObject2.put("value",list.get(0).get("longTime"));
        indiObject2.put("rate_level","red");
        JSONObject nameObject2 = new JSONObject();
        nameObject2.put("name","运输时长");
        indiArray2.add(indiObject2);
        indiArray2.add(nameObject2);
        dataObject2.put("indicators",indiArray2);
        dataArray.add(dataObject2);
        jsonObject.put("data",dataArray);

        return jsonObject;
    };
    //统计当日物流下单次数 sql
    @RequestMapping("/countTotal")
    public Object countTotal(@RequestParam("nowDate") String nowDate ){
        List<Map> list = tmsService.countTotal(nowDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        jsonObject.put("data",list.get(0).get("total"));
        return jsonObject;
    };
    //统计完运时长存储过程 sql
    @RequestMapping("/longTime")
    public Object longTime(@RequestParam("nowDate") String nowDate ){
        List<Map> list = tmsService.longTime(nowDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");

        JSONObject dataObject = new JSONObject();

        JSONArray colArray = new JSONArray();
        JSONObject colObject = new JSONObject();
        colObject.put("name","机构");
        colObject.put("id","subsys");
        colArray.add(colObject);


        JSONObject colObject1 = new JSONObject();
        colObject1.put("name","完运时长");
        colObject1.put("id","module");
        colArray.add(colObject1);

        JSONArray rowArray = new JSONArray();
        for (Map map : list) {
            JSONObject rowObject = new JSONObject();
            rowObject.put("subsys",map.get("NAME"));
            rowObject.put("module",map.get("longTime"));
            rowArray.add(rowObject);
        }


        dataObject.put("columns",colArray);
        dataObject.put("rows",rowArray);
        jsonObject.put("data",dataObject);
        return jsonObject;
    };
    //统计各转运站派送成功数 sql
    @RequestMapping("/organTotal")
    public Object organTotal(@RequestParam("nowDate") String nowDate ){
        List<Map> list = tmsService.organTotal(nowDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONObject dataObject = new JSONObject();
        JSONArray catArray = new JSONArray();
        for (Map map : list) {
            catArray.add(map.get("NAME")) ;
        }
        dataObject.put("categories",catArray);

        JSONArray serArray = new JSONArray();
        JSONObject serObject = new JSONObject();
        serObject.put("name","各转运站派送成功数");

        JSONArray dataArray = new JSONArray();
        for (Map map : list) {
            dataArray.add(map.get("total")) ;
        }
        serObject.put("data",dataArray);
        serArray.add(serObject);
        dataObject.put("series",serArray);
        jsonObject.put("data",dataObject);
        return jsonObject;
    };
}
